package com.demo.delayed_queue;

import cn.lingyangwl.framework.mq.base.manager.MqManager;
import com.demo.commons.MqCons;
import com.demo.commons.message.OrderMessage;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 订单超时处理
 * 死信交换机
 * 定义死信队列
 * @author shenguangyang
 */
@Component
@RabbitListener(bindings = {
        @QueueBinding(value = @Queue(value = MqCons.ORDER_DEAD_QUEUE01),
                exchange = @Exchange(value = MqCons.ORDER_DEAD_EXCHANGE01))
})
public class OrderClose01Listener {
    private static final Logger log = LoggerFactory.getLogger(OrderClose01Listener.class);

    @Resource
    private MqManager mqManager;

    @RabbitHandler
    public void onMessage(OrderMessage bizMessage, Channel channel, Message message) {
        try {
            // 使用rabbitEmailMessage 进行业务处理
            log.info("{}", bizMessage);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);

                // channel.basicRecover(true);
            } catch (Exception ex) {
                log.error("basicNack fail: {}", ex.getMessage());
                return;
            }
            log.error("exec fail: {}", e.getMessage());
        }
    }
}
